//
//  HRShowSayHelloView.m
//  Hear
//
//  Created by mac on 2017/10/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowSayHelloView.h"
#import "HRDynamicCell.h"
#import "HRDynamicCommentModel.h"
#import "HRUpdataUserInfoModel.h"

@interface HRShowSayHelloView ()
@property (nonatomic, copy)SenderVoiceBlock senderVoiceBlock;
@property(nonatomic, strong)HRSoundButton *soundButton;

@end
@implementation HRShowSayHelloView
+(void)showShowSayHelloView:(SenderVoiceBlock)senderVoiceBlock{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowSayHelloView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowSayHelloView class]]){
        
        
        HRShowSayHelloView *shareView = (HRShowSayHelloView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadshowShowSayHelloView:senderVoiceBlock];
        
    }
}

- (void)loadshowShowSayHelloView:(SenderVoiceBlock)senderVoiceBlock{
    WeakSelf
    self.senderVoiceBlock = senderVoiceBlock;
//    UIButton *closeButton = [UIButton buttonWithType:0];
//    closeButton.frame = self.bounds;
//    [closeButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:closeButton];
    
    self.alpha = 0;
    CGFloat backWidth = 240;
    CGFloat backHeight = 200;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((kFullWidth-backWidth)/2, (kFullHeight-backHeight)/2, backWidth, backHeight)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.alpha = 0;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.3 animations:^{
        weakSelf.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        weakSelf.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    UIImageView *backImageView = [UIImageView new];
    backImageView.image = [UIImage imageNamed:@"圆角矩形-30"];
    [contentView addSubview:backImageView];
    backImageView.frame = contentView.bounds;
    backImageView.userInteractionEnabled = YES;
  
    CGFloat pading = 20;
    HRSoundButton *soundButton = [[HRSoundButton alloc] initWithFrame:CGRectMake((contentView.width-170)/2, pading, 170, 50)];
    [backImageView addSubview:(self.soundButton=soundButton)];
    HRDynamicCommentModel *commentModel = [HRDynamicCommentModel new];
    HRUpdataUserInfoModel*model = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
    commentModel.SoundUrl = model.UserSoundUrl;
    commentModel.SoundLength = model.SoundLength;
    soundButton.commentModel = commentModel;
    soundButton.playCommentMedioBlock = ^(HRDynamicCommentModel *commentModel) {
        weakSelf.soundButton.commentModel = commentModel;
    };
    
    
    UIButton *recodButton = [UIButton buttonWithType:0];
    [recodButton setTitle:@"重新录制" forState:0];
    [recodButton setTitleColor:SecondTextColor forState:0];
    recodButton.titleLabel.font = CustomUIFont(14);
    [recodButton addTarget:self action:@selector(recodButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backImageView addSubview:recodButton];
    recodButton.frame = CGRectMake(self.soundButton.right-60, self.soundButton.bottom, 60, 20);
    
    NSArray *textArr = @[@"取消",@"发送"];
    for (int i =0; i < textArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:0];
        [button setTitle:textArr[i] forState:0];
        [button setTitleColor:SecondTextColor forState:0];
        button.titleLabel.font = CustomUIFont(15);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [backImageView addSubview:button];
        button.frame = CGRectMake(self.soundButton.left+i*(70+20), recodButton.bottom+pading, 70, 35);
        button.tag = i;
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 5;
        if (i ==0){
            button.layer.borderColor = SecondTextColor.CGColor;
        }else{
            [button setTitleColor:CustomColor(240, 113, 116, 1) forState:0];
            button.layer.borderColor = CustomColor(240, 113, 116, 1).CGColor;
        }
    }
    
}

- (void)recodButtonClick{
    
}

- (void)buttonClick:(UIButton *)button{
    if (button.tag == 0){
        [self closeView];
    }else{
        self.senderVoiceBlock();
    }
}




-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
